/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "Expression3DForm.h"
#include "ui_Expression3DForm.h"
#include "Expression3D.h"
#include <QMessageBox>
Expression3DForm::Expression3DForm(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::Expression3DForm)
{
    _modified = false;
    _expression = 0;
    m_ui->setupUi(this);
    connect(m_ui->nameEdit,SIGNAL(textEdited(QString)),this,SLOT(onNameChanged(QString)));
    connect(m_ui->varEdit,SIGNAL(textEdited(QString)),this,SLOT(onVarsChanged(QString)));
    connect(m_ui->xEdit,SIGNAL(textEdited(QString)),this,SLOT(onXExpressionChanged(QString)));
    connect(m_ui->yEdit,SIGNAL(textEdited(QString)),this,SLOT(onYExpressionChanged(QString)));
    connect(m_ui->zEdit,SIGNAL(textEdited(QString)),this,SLOT(onZExpressionChanged(QString)));
}

Expression3DForm::~Expression3DForm()
{
    delete m_ui;
}

void Expression3DForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void Expression3DForm::setExpression(Expression3D* expr){
    if(_modified){
        QMessageBox msg;
        msg.setText(tr("The setting for the current object have been changed..."));
        msg.setInformativeText(tr("Do you want to apply changes?"));
        msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msg.setDefaultButton(QMessageBox::Yes);
        int ret = msg.exec();
        switch (ret) {
            case QMessageBox::Yes:
            apply();
            break;
            case QMessageBox::No:
            break;
            default:
            break;
        }
    }
    _expression = expr;
    init();
}

void Expression3DForm::init(){
    if(_expression){
        _expressionName = _expression->objectName();
        _vars = "";
        int n = _expression->variablesList().count();
        for(int i = 0; i < n;++i){
            _vars+=_expression->variablesList()[i];
            if(i!=n-1)
                _vars+=",";
        }
        _xExpression = _expression->x();
        _yExpression = _expression->y();
        _zExpression = _expression->z();
        m_ui->nameEdit->setText(_expressionName);
        m_ui->xEdit->setText(_xExpression);
        m_ui->yEdit->setText(_yExpression);
        m_ui->zEdit->setText(_zExpression);
        m_ui->varEdit->setText(_vars);
        _modified = false;
    }
}

void Expression3DForm::apply(){
    if(_expression){
        _expression->setObjectName(_expressionName);
        _expression->setVariablesList(_vars.split(","));
        _expression->setX(_xExpression);
        _expression->setY(_yExpression);
        _expression->setZ(_zExpression);
        _modified = false;
        emit modified(_modified);
    }
}

void Expression3DForm::revert(){
    if(_expression){
        init();
        emit modified(_modified);
    }
}

void Expression3DForm::onNameChanged(const QString& name){
    if(_expression){
        _expressionName = name;
        _modified = true;
        emit modified(_modified);
    }
}
void Expression3DForm::onVarsChanged(const QString& vars){
    if(_expression){
        _vars = vars;
        _modified = true;
        emit modified(_modified);
    }
}
void Expression3DForm::onXExpressionChanged(const QString& expr){
    if(_expression){
        _xExpression = expr;
        _modified = true;
        emit modified(_modified);
    }
}
void Expression3DForm::onYExpressionChanged(const QString& expr){
    if(_expression){
        _yExpression = expr;
        _modified = true;
        emit modified(_modified);
    }
}
void Expression3DForm::onZExpressionChanged(const QString& expr){
    if(_expression){
        _zExpression = expr;
        _modified = true;
        emit modified(_modified);
    }
}
